﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace Library
{
    public delegate void SelectItem (object sender, EventArgs e);

    public partial class NewArticle : Form
    {
        public ListBox IssueNum_ltbx { get { return MagazIssueNumber_ltbx; } }
        public List<Article> newArticles { get; private set; }
        public event SelectItem SelectMagName;


        public NewArticle(List<string> magazinesNames)
        {
            InitializeComponent();
            MagazName_ltbx.DataSource = magazinesNames;
            newArticles = new List<Article>();

        }
        private void AddArt_btn_Click(object sender, EventArgs e)
        {
            string name = NameArt_txbx.Text.Trim();
            string publisher = PublisherArt_txbx.Text.Trim();
            DateTime publishedDate = PublishedDateArt_dtp.Value;
            
            string version = Version_txbx.Text.Trim();
            string authorName = AuthorNameArt_txbx.Text.Trim();
            string magName = MagazName_ltbx.SelectedValue.ToString();
            string magIssueNumber = string.Empty;
            if (MagazIssueNumber_ltbx.Items !=null && MagazIssueNumber_ltbx.SelectedItem !=null)
                magIssueNumber = MagazIssueNumber_ltbx.SelectedValue.ToString();


            if (name == string.Empty ||
                publisher == string.Empty ||
                publishedDate == null ||
                version == string.Empty ||
                authorName == string.Empty ||
                magName == string.Empty ||
                magIssueNumber == string.Empty)
            {
                MessageBox.Show("Вы не указали все поля.");
                return;
            }
            foreach (Article art in newArticles)
            {
                if (name == art.Name && magName == art.MagazineName && magIssueNumber == art.MagazineIssueNumber)
                {
                    MessageBox.Show("Такая статья уже добавлена");
                    return;
                }

            }
            newArticles.Add(new Article(name, publisher, publishedDate, magName, magIssueNumber, authorName, version));
            addedArt_lbl.Text = "Добавлено: " + newArticles.Count;
        }
        private void SaveArt_btn_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        private void MagazName_ltbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectMagName != null)
            {
                SelectMagName(sender, e);
            }
        }

    }
}
